<template>
  <UiFormItem :class="{ hidden: info.extension.hidden.value || conditionProperty.hidden }" :info="info">
    <div class="flex">
      <!-- <el-select v-model="formDataModel" size="mini">
        <el-option v-for="item in printersOptions" :key="item.value" :label="item.label" :value="item.value" />
      </el-select>
      <label
        class="flex flex-none justify-end items-center text-right text-xs text-gray-500 pr-[10px] pl-1"
        :style="{
          width: `${info.extension.paperLabelWidth.value}px`,
        }"
      >
        {{ info.extension.paperLabel.value }}
      </label>
      <el-select v-model="formDataModel2" size="mini">
        <el-option v-for="item in printerPapersOptions" :key="item.value" :label="item.label" :value="item.value" />
      </el-select> -->
      <el-button size="mini" type="primary" @click="btnOpen">
        {{ formDataModel ? `${info.extension.btnEdit.value}` : `${info.extension.btnAdd.value}` }}
      </el-button>
    </div>
  </UiFormItem>
</template>
<script>
  import { defineComponent } from 'vue'
  import { UiMixin, ElementsFormMixin, RefsInjectMixin, ConditionMixin } from '@ve/mixin'
  import { newRun } from '@ve/utils/print'
  import request from '@/utils/request'
  import { GFCD } from '@/utils/utils'
  // import { webapp_onmessage } from '@ve/utils/print/grwebapp'
  export default defineComponent({
    name: 'UiPrintTemplate',
    mixins: [UiMixin, ElementsFormMixin, RefsInjectMixin, ConditionMixin],
    computed: {
      getPrintMessage() {
        return this.$store.getters['diyPrint/getMessage']
      },
    },
    watch: {
      formDataModel: {
        deep: true,
        handler(newval) {
          if (!newval && newval !== 0) return
          // if (this.info.extension.type.value == 'textarea') {
          //   this.formDataModel = newval.split('\r\n')[0].split('\n').join(',')
          // } else {
          this.formDataModel = String(newval)
          // }
        },
      },
      getPrintMessage(val) {
        if (val) {
          this.setData(val)
        }
      },
    },
    created() {},
    mounted() {
      this.init()
    },
    methods: {
      async getPrintData() {
        let res = await request({
          // url: 'http://192.168.3.211/wms/wms-web',
          url: GFCD('dams', 'domain') + this.$store.getters['user/appCodeUrl'],
          method: 'post',
          data: {
            data: JSON.stringify({ FILE_SEQ: this.formDataModel, '#OUTPUT_TYPE#': 'stream' }),
            method: this.info.extension?.downloadMethod?.value ? this.info.extension?.downloadMethod?.value : 'wms.file.common.download',
          },
          // responseType: 'blob',
        })
        console.log(res)
        newRun('edit', res)
      },
      btnOpen() {
        if (this.formDataModel) {
          if (!isNaN(Number(this.formDataModel))) {
            this.getPrintData()
          } else {
            newRun('edit', this.formDataModel)
          }
        } else {
          newRun('create', '')
        }
      },
      setData(event) {
        if (!this.hasPreview) {
          console.log('预览模式不运行 打印机')
          return
        }
        let message = JSON.parse(event.data)
        if (message.type == 'edit') {
          this.formDataModel = message.result
        }
      },
      init() {
        if (!this.hasPreview) {
          console.log('预览模式不运行 打印机')
          return
        }
        // 新的
      },
    },
  })
</script>
